//
//  PwInputView.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/7/1.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "PwInputView.h"

@interface PwInputView ()

@property(nonatomic, weak)UITextField *textField;
@property(nonatomic, strong)NSMutableArray *passwords;

@end

@implementation PwInputView


- (instancetype)init{
    
    if (self = [super init]) {
       
        [self establishSubViews];
        
    }
    return self;
}


- (void)establishSubViews{
    
    UITextField *textField = [[UITextField alloc] init];
    textField.tintColor = [UIColor clearColor];
    textField.textColor = [UIColor clearColor];
    textField.keyboardType = UIKeyboardTypeNumberPad;
    [self addSubview:textField];
    self.textField = textField;
    self.textField.backgroundColor = [UIColor clearColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChange) name:UITextFieldTextDidChangeNotification object:nil];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    UILabel *label = [[UILabel alloc] init];
    label.text = XHLocalizedString(@"Input 4 locking/unlocking codes");
    label.font = [UIFont systemFontOfSize:17];
    label.textColor = [UIColor colorFromHexCode:@"#666666"];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_offset(0);
        make.top.mas_offset(30);
        
    }];
    NSMutableArray *imageViews = [NSMutableArray arrayWithCapacity:4];
    self.passwords = [NSMutableArray array];
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"key_password"]];
        [self addSubview:imageView];
        [imageViews addObject:imageView];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"*";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor colorFromHexCode:@"#333333"];
        label.font = [UIFont systemFontOfSize:40];
        label.hidden = YES;
        [self addSubview:label];
        [self.passwords addObject:label];
        
    }
    [imageViews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(label.mas_bottom).offset(30);
        make.width.height.mas_offset(50);
        
    }];
    [imageViews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:25 tailSpacing:25];
   
    [self.passwords mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(label.mas_bottom).offset(38);
        
    }];
    [self.passwords mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:25 tailSpacing:25];
    
}

- (void)textFieldTextChange{
    
    NSString *password = self.textField.text;
    if (password.length == 4) {
        
        if ([_delegate respondsToSelector:@selector(pwInputView:inputEndWithtext:)]) {
           [self.delegate pwInputView:self inputEndWithtext:password];
        }
          [self endEditing:YES];
    }
    [self.passwords enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (idx < password.length) {
            
            label.hidden = NO;
            
        }else{
            
            label.hidden = YES;
        }
        
    }];
    
    
    
}

- (void)clearPassword{
    
    self.textField.text = @"";
    [self textFieldTextChange];
    
}

@end
